package item.inkcloud.jwtservice.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class JwtUtils {

    @Value("${jwt.secret}")
    private String secret ;

    @Value("${jwt.validTime}")
    private int validTime;


    public String getToken(String userid ){


        Date date = new Date();
        Date expire = new Date(date.getTime() + validTime);

        String token = Jwts.builder().claim("userid",userid)
                .setHeaderParam("typ", "JWT")
                .setIssuedAt(date)
                .setExpiration(expire)
                .signWith(SignatureAlgorithm.HS512,secret)
                .compact();


        return token;
    }


    public Claims getClaimByToken(String token) {
        try {
            return Jwts.parser()
                    .setSigningKey(secret)
                    .parseClaimsJws(token)
                    .getBody();
        } catch (Exception e) {
            return null;
        }

    }



    public boolean isTokenExpired(Date expiration) {
        return expiration.before(new Date());
    }





}